<?
require_once("cms/core/action/AbstractListAction.php");

class ListAction extends AbstractListAction {

    private $log;

    public function __construct() {
        $this->log =& LoggerManager::getLogger("ListAction");
    }

    public function execute(ActionConfig &$mapping, &$form, Request &$request, Response &$response) {

        if($form->getId() === null)
            return $mapping->findForward("noid");

        return parent::execute($mapping, $form, $request, $response);
    }

    public function activate(ActionConfig &$mapping, &$form, Request &$request, Response &$response) {

        $this->log->debug("Activating gallery images");
        $c = new Criteria();
        $c->add(GalleryImagePeer::GALLERY_IMAGE_ID, $form->getItems(), Criteria::IN);
        $c->add(GalleryImagePeer::ACTIVE, true);
        try {
            GalleryImagePeer::doUpdate($c);
            $this->messages->add(ActionMessage::GLOBAL_MESSAGE,
                new ActionMessage("list.success.activate", true));
	        if($request->getContext()->getInitParameter(_Core::CACHE)) {
	            // Get the cache
	            $cache = $request->getContext()->getCache();
	            // Clear the menu cache
	            $cache->clean(Zend_Cache::CLEANING_MODE_MATCHING_TAG, array('menu'));              
	            // Clear the page cache
	            $cache->clean(Zend_Cache::CLEANING_MODE_MATCHING_TAG, array('page'));
	        }
        } catch(PropelException $e) {
            $this->errors->add(ActionMessage::GLOBAL_MESSAGE,
                new ActionMessage("list.error.activate", true));
            $this->log->error($e->getMessage());
        }

    }

    public function deactivate(ActionConfig &$mapping, &$form, Request &$request, Response &$response) {

        $this->log->debug("Deactivating gallery images");
        $c = new Criteria();
        $c->add(GalleryImagePeer::GALLERY_IMAGE_ID, $form->getItems(), Criteria::IN);
        $c->add(GalleryImagePeer::ACTIVE, false);
        try {
            GalleryImagePeer::doUpdate($c);
            $this->messages->add(ActionMessage::GLOBAL_MESSAGE,
                new ActionMessage("list.success.deactivate", true));
	        if($request->getContext()->getInitParameter(_Core::CACHE)) {
	            // Get the cache
	            $cache = $request->getContext()->getCache();
	            // Clear the menu cache
	            $cache->clean(Zend_Cache::CLEANING_MODE_MATCHING_TAG, array('menu'));              
	            // Clear the page cache
	            $cache->clean(Zend_Cache::CLEANING_MODE_MATCHING_TAG, array('page'));
	        }
        } catch(PropelException $e) {
            $this->errors->add(ActionMessage::GLOBAL_MESSAGE,
                new ActionMessage("list.error.deactivate", true));
            $this->log->error($e->getMessage());
        }

    }

    public function delete(ActionConfig &$mapping, &$form, Request &$request, Response &$response) {

        $this->log->debug("Deleting gallery images");
        $c = new Criteria();
        $c->add(GalleryImagePeer::GALLERY_IMAGE_ID, $form->getItems(), Criteria::IN);

        $conn = Propel::getConnection();

        try {

            $conn->begin();

            require_once("cms/core/om/BinaryFilePeer.php");
            require_once("cms/core/om/ImagePeer.php");

            $galleryImages = GalleryImagePeer::doSelect($c, $conn);

            foreach($galleryImages as $galleryImage) {

                $thumb = $galleryImage->getImageRelatedByThumbnailId();
                $image = $galleryImage->getImageRelatedByImageId();

                // Move all higher positions one up.
                $query =
                "UPDATE gallery_image SET position = position - 1 ".
                "WHERE gallery_id = " .$galleryImage->getGalleryId()." ".
                "AND position > " . $galleryImage->getPosition();

                $stmt = $conn->createStatement();
                $stmt->executeUpdate($query, $conn);

                // Delete the galleryImage
                GalleryImagePeer::doDelete($galleryImage, $conn);

                // Delete the thumb (image will go through ondelete="cascade")
                $c = new Criteria();
                $c->add(BinaryFilePeer::BINARY_FILE_ID, $thumb->getBinaryFileId());
                BinaryFilePeer::doDelete($c, $conn);

                // Delete the binaryFile (image will go through ondelete="cascade")
                $c = new Criteria();
                $c->add(BinaryFilePeer::BINARY_FILE_ID, $image->getBinaryFileId());
                BinaryFilePeer::doDelete($c, $conn);

            }

            $this->messages->add(ActionMessage::GLOBAL_MESSAGE,
                new ActionMessage("list.success.delete", true));
                
	        if($request->getContext()->getInitParameter(_Core::CACHE)) {
	            // Get the cache
	            $cache = $request->getContext()->getCache();
	            // Clear the menu cache
	            $cache->clean(Zend_Cache::CLEANING_MODE_MATCHING_TAG, array('menu'));              
	            // Clear the page cache
	            $cache->clean(Zend_Cache::CLEANING_MODE_MATCHING_TAG, array('page'));
	        }

            $conn->commit();

        } catch(PropelException $e) {
            $conn->rollback();
            $this->errors->add(ActionMessage::GLOBAL_MESSAGE,
                new ActionMessage("list.error.delete", true));
            $this->log->error($e->getMessage());
        }

    }

    public function moveup(ActionConfig &$mapping, &$form, Request &$request,
            Response &$response) {
        $this->move($form, "up", $request);
    }

    public function movedown(ActionConfig &$mapping, &$form, Request &$request,
            Response &$response) {
        $this->move($form, "down", $request);
    }

    private function move($form, $direction, Request $request) {

        $items = $form->getItems();

        $galleryImage = GalleryImagePeer::retrieveByPk($items[0]);

        if(is_null($galleryImage)) {
            $this->errors->add(ActionMessage::GLOBAL_MESSAGE, new ActionMessage("Error finding image."));
            return;
        }

        $conn = Propel::getConnection();
        $stmt = $conn->createStatement();

        try {

            $conn->begin();

            $query =
                "UPDATE gallery_image SET position = position ".($direction == "down" ? "-" : "+")." 1 ".
                "WHERE ".
                "gallery_id = ".$galleryImage->getGalleryId()." ".
                "AND position = ".($direction == "down" ? $galleryImage->getPosition() + 1 : $galleryImage->getPosition() - 1);

            $stmt->executeUpdate($query, $conn);

            $query =
                "UPDATE gallery_image SET position = position ".($direction == "down" ? "+" : "-")." 1 ".
                "WHERE ".
                "gallery_id = ".$galleryImage->getGalleryId()." ".
                "AND gallery_image_id = ".$galleryImage->getGalleryImageId();

            $stmt->executeUpdate($query, $conn);
            
	        if($request->getContext()->getInitParameter(_Core::CACHE)) {
	            // Get the cache
	            $cache = $request->getContext()->getCache();
	            // Clear the menu cache
	            $cache->clean(Zend_Cache::CLEANING_MODE_MATCHING_TAG, array('menu'));              
	            // Clear the page cache
	            $cache->clean(Zend_Cache::CLEANING_MODE_MATCHING_TAG, array('page'));
	        }

            $conn->commit();

        } catch(PropelException $e) {
            $conn->rollback();
            $errors->add(ActionMessage::GLOBAL_MESSAGE,
                new ActionMessage("list.error.move", true));
            $this->log->error($e->getMessage());
        }

    }

}
?>